﻿using d1Common;
using d1Common.WebController;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Common.do4A.Controllers.a2
{
    [Route("do4A/a2/MyInformation")]
    public class MyInformationies2Controller : Authorize2Controller
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private do4ADbContext db = new do4ADbContext();
        public async Task<IHttpActionResult> Get()
        {
            string fileAllPath = doServices.Instance.DoStorage.getAccessUrl("");
            var entity = await db.User2s.Where(x => x.Id == this.UserId)
                .Select(x => new
                {
                    x.Id,
                    x.Code,
                    x.Name,
                    x.Icon,
                    IconLong = fileAllPath + x.Icon,
                    x.Email,
                    x.Sex,
                    x.MobilePhone,
                    x.IdCard,
                    x.IsAvailable,
                    x.Address,
                    x.BirthDay,
                    x.CreateTime,
                }).FirstOrDefaultAsync();
            if (entity == null) this.Error("当前用户不存在");
            return Ok(entity);
        }

        public async Task<IHttpActionResult> Put(MyInformationPutVm model)
        {
            string fileAllPath = doServices.Instance.DoStorage.getAccessUrl("");
            var entity = await db.User2s.FindAsync(this.UserId);
            if (entity == null) this.Error("当前用户不存在");
            if (entity.MobilePhone != model.MobilePhone)
            {
                var oldMobilePhone = await db.User2s.Where(t => t.MobilePhone == model.MobilePhone).FirstOrDefaultAsync();
                if (oldMobilePhone != null) this.Error("电话重复");
                entity.MobilePhone = model.MobilePhone;
            }
            entity.Code = model.Code;
            entity.Name = model.Name;
            entity.Icon = model.Icon;
            entity.BirthDay = model.BirthDay;
            entity.Sex = model.Sex;
            entity.IdCard = model.IdCard;
            entity.Address = model.Address;
            await db.SaveChangesAsync();
            return Ok();
        }
    }

    public class MyInformationPutVm
    {
        /// <summary>
        /// 编码 
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public DateTime? BirthDay { get; set; }

        /// <summary>
        /// 手机号
        /// </summary>
        public string MobilePhone { get; set; }

        /// <summary>
        /// 性别： 1：男 0：女
        /// </summary>
        public string Sex { get; set; }
        /// <summary>
        /// 身份证号
        /// </summary>
        public string IdCard { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }
    }
}
